<?php
class PdtDoituongsController extends AppController {

	var $name = 'PdtDoituongs';
	var $helpers = array('Html', 'Form');
	var $paginate = array('limit' =>10, 'page' => 1);
	function index() {
		$this->PdtDoituong->recursive = 0;
		$this->set('pdtDoituongs', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid PdtDoituong.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('pdtDoituong', $this->PdtDoituong->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->PdtDoituong->create();
			$pdt_id = $this->data['PdtDoituong']['phieu_dt_id'];
			foreach($this->data['PdtDoituong']['doituong_id'] as $key=>$value){
				$record['PhieuDt']['id'] = $pdt_id;
				$record['Doituong']['id'] = $value;
				if (!$this->PdtDoituong->saveAll($record)) {
					$this->Session->setFlash(__('Thông tin chưa được lưu. Bạn vui lòng nhập lại.', true));
					$this->redirect(array('action'=>'index'));
				}
			}
			$this->Session->setFlash(__('Thông tin đã được lưu', true));
			$this->redirect(array('action'=>'index'));
			
		}
		$phieuDts = $this->PdtDoituong->PhieuDt->find('list');
		$doituongs = $this->PdtDoituong->Doituong->find('list',array('fields'=>array('Doituong.id','TEN_DT')));
		$this->set(compact('phieuDts', 'doituongs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid PdtDoituong', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->PdtDoituong->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã được lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Thông tin chưa được lưu. Bạn vui lòng nhập lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->PdtDoituong->read(null, $id);
		}
		$phieuDts = $this->PdtDoituong->PhieuDt->find('list');
		$doituongs = $this->PdtDoituong->Doituong->find('list');
		$this->set(compact('phieuDts','doituongs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for PdtDoituong', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->PdtDoituong->del($id)) {
			$this->Session->setFlash(__('Thao tác xóa hoàn tất', true));
			$this->redirect(array('action'=>'index'));
		}
	}


	function admin_index() {
		$this->PdtDoituong->recursive = 0;
		$this->set('pdtDoituongs', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid PdtDoituong.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('pdtDoituong', $this->PdtDoituong->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->PdtDoituong->create();
			if ($this->PdtDoituong->save($this->data)) {
				$this->Session->setFlash(__('The PdtDoituong has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The PdtDoituong could not be saved. Please, try again.', true));
			}
		}
		$phieuDts = $this->PdtDoituong->PhieuDt->find('list');
		$doituongs = $this->PdtDoituong->Doituong->find('list');
		$this->set(compact('phieuDts', 'doituongs'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid PdtDoituong', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->PdtDoituong->save($this->data)) {
				$this->Session->setFlash(__('The PdtDoituong has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The PdtDoituong could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->PdtDoituong->read(null, $id);
		}
		$phieuDts = $this->PdtDoituong->PhieuDt->find('list');
		$doituongs = $this->PdtDoituong->Doituong->find('list');
		$this->set(compact('phieuDts','doituongs'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for PdtDoituong', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->PdtDoituong->del($id)) {
			$this->Session->setFlash(__('PdtDoituong deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>